package com.charm.bulkcomponent.ui.activity

import android.annotation.TargetApi
import android.content.pm.ActivityInfo
import android.os.Build
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.widget.ImageView
import com.alibaba.android.arouter.facade.annotation.Route
import com.charm.bulkcomponent.R
import com.charm.codes.video.ArouPickVideo
import com.charm.codes.video.entity.SwitchVideoModel
import com.shuyu.gsyvideoplayer.utils.OrientationUtils
import com.shuyu.gsyvideoplayer.video.base.GSYVideoPlayer
import java.util.ArrayList

/**
 * Created by tools on 2018/1/9.
 */
@Route(path = "/common/banner/video")
class BannerVideoActivity : AppCompatActivity() {

    private lateinit var videoPlayer: ArouPickVideo
    private lateinit var orientationUtils : OrientationUtils

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_banner_video)
        videoPlayer = findViewById(R.id.video)
        initData()
    }

    private fun initData() {
        val url = "https://res.exexm.com/cw_145225549855002"

        //String url = "http://7xse1z.com1.z0.glb.clouddn.com/1491813192";
        //需要路径的
        //videoPlayer.setUp(url, true, new File(FileUtils.getPath()), "");

        //借用了jjdxm_ijkplayer的URL
        val source1 = "http://9890.vod.myqcloud.com/9890_4e292f9a3dd011e6b4078980237cc3d3.f20.mp4"
        val name = "普通"
        val switchVideoModel = SwitchVideoModel(name, source1)

        val source2 = "http://9890.vod.myqcloud.com/9890_4e292f9a3dd011e6b4078980237cc3d3.f30.mp4"
        val name2 = "清晰"
        val switchVideoModel2 = SwitchVideoModel(name2, source2)

        val list = ArrayList<SwitchVideoModel>()
        list.add(switchVideoModel)
        list.add(switchVideoModel2)

        videoPlayer.setUp(list, true, "测试视频")

        //增加封面
        val imageView = ImageView(this)
        imageView.scaleType = ImageView.ScaleType.CENTER_CROP
//        imageView.setImageResource(R.mipmap.xxx1)
        videoPlayer.thumbImageView = imageView
        //增加title
        videoPlayer.titleTextView.visibility = View.VISIBLE
        //设置返回键
        videoPlayer.backButton.visibility = View.VISIBLE
        //设置旋转
        orientationUtils = OrientationUtils(this, videoPlayer)

        //设置全屏按键功能,这是使用的是选择屏幕，而不是全屏
        videoPlayer.fullscreenButton.setOnClickListener({ orientationUtils.resolveByClick() })
        //是否可以滑动调整
        videoPlayer.setIsTouchWiget(true)

        //设置返回按键功能
        videoPlayer.backButton.setOnClickListener({ onBackPressed() })

        //过渡动画
    }

    override fun onPause() {
        super.onPause()
        videoPlayer.onVideoPause()
    }

    override fun onResume() {
        super.onResume()
        videoPlayer.onVideoResume()
    }

    @TargetApi(Build.VERSION_CODES.KITKAT)
    override fun onDestroy() {
        super.onDestroy()
        if (orientationUtils != null)
            orientationUtils.releaseListener()
    }

    override fun onBackPressed() {
        //先返回正常状态
        if (orientationUtils.screenType == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
            videoPlayer.fullscreenButton.performClick()
            return
        }
        //释放所有
//        videoPlayer.setStandardVideoAllCallBack(null)
//        GSYVideoPlayer.releaseAllVideos()
        super.onBackPressed()
//        if (isTransition && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            super.onBackPressed()
//        } else {
//            Handler().postDelayed({
//                finish()
//                overridePendingTransition(R.anim.abc_fade_in, R.anim.abc_fade_out)
//            }, 500)
//        }
    }


//    private fun initTransition() {
//        if (isTransition && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            postponeEnterTransition()
//            ViewCompat.setTransitionName(videoPlayer, IMG_TRANSITION)
//            addTransitionListener()
//            startPostponedEnterTransition()
//        } else {
//            videoPlayer.startPlayLogic()
//        }
//    }

//    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
//    private fun addTransitionListener(): Boolean {
//        transition = window.sharedElementEnterTransition
//        if (transition != null) {
//            transition.addListener(object : OnTransitionListener() {
//                fun onTransitionEnd(transition: Transition) {
//                    super.onTransitionEnd(transition)
//                    videoPlayer.startPlayLogic()
//                    transition.removeListener(this)
//                }
//            })
//            return true
//        }
//        return false
//    }
}